


#include "IntervalSelector.h"


IntervalSelector::IntervalSelector(string attributeName, Type* points, int n) {

	if(points[0].type == INT) { MINUS_INF.intVal = -1000000;	PLUS_INF.intVal = 1000000; }
	if(points[0].type == FLOAT) { MINUS_INF.floatVal = -1000000;	PLUS_INF.floatVal = 1000000; }
	intervalNumber = n+1;
	intervals = new Interval[intervalNumber];
//	cout << "intervalli: ";
	intervals[0].start = MINUS_INF;
	intervals[0].end = points[0];
	intervals[0].encodingString = attributeName +'0';
	cout << intervals[0].encodingString << "(" << intervals[0].start.intVal << "," << intervals[0].end.intVal << ") ";
	for (int i=1; i<n; i++){
		intervals[i].start = points[i-1];
		intervals[i].end = points[i];
		intervals[i].encodingString = attributeName + util::intToString(i);
		cout << intervals[i].encodingString << "(" << intervals[i].start.intVal << "," << intervals[i].end.intVal << ") ";
	}
	intervals[n].start = points[n-1];
	intervals[n].end = PLUS_INF;
	intervals[n].encodingString = attributeName + util::intToString(n);
	cout << intervals[n].encodingString << "(" << intervals[n].start.intVal << "," << intervals[n].end.intVal << ") "; // << endl;
}


IntervalSelector::~IntervalSelector(){

}


set<std::string> IntervalSelector::getMatchingIntervals(Constraint constraint){

	std::set<std::string> matchedStrings;
	for (int i=0; i<intervalNumber; i++){
		if (overlap(constraint, intervals[i])){
			matchedStrings.insert(intervals[i].encodingString);
		}
	}
	return matchedStrings;
}


std::string IntervalSelector::getMatchingIntervals(Attribute attribute){

	std::string matchedString;
	for (int i=0; i<intervalNumber; i++){
		if (hit(attribute, intervals[i])){
			matchedString = intervals[i].encodingString;
			break;
		}
	}
	return matchedString;
}


bool IntervalSelector::overlap(Constraint constraint, Interval interval){

	if (constraint.type == INT) {
		int point = constraint.intVal;
		if (constraint.op == EQ){
			if ((point>interval.start.intVal) && (point<=interval.end.intVal)) {
				return true;
			} else {
				return false;
			}
		}
		if (constraint.op == GT){
			if (interval.end.intVal > point){
				return true;
			} else {
				return false;
			}
		}
		if (constraint.op == LT) {
			if (interval.start.intVal < point){
				return true;
			} else {
				return false;
			}
		}
	}

	if (constraint.type == FLOAT) {
		float point = constraint.floatVal;
		if (constraint.op == EQ){
			if ((point>interval.start.floatVal) && (point<=interval.end.floatVal)) {
				return true;
			} else {
				return false;
			}
		}
		if (constraint.op == GT){
			if (interval.end.floatVal > point){
				return true;
			} else {
				return false;
			}
		}
		if (constraint.op == LT) {
			if (interval.start.floatVal < point){
				return true;
			} else {
				return false;
			}
		}
	}
	return false;
}


bool IntervalSelector::hit(Attribute attribute, Interval interval){

	if (attribute.type == INT) {
		int point = attribute.intVal;
		if ((point>interval.start.intVal) && (point<=interval.end.intVal)) {
			return true;
		} else {
			return false;
		}
	}

	if (attribute.type == FLOAT) {
		float point = attribute.floatVal;
		if ((point>interval.start.floatVal) && (point<=interval.end.floatVal)) {
			return true;
		} else {
			return false;
		}
	}
	return false;
}



